function ajax(
    type, 
    url, 
    oData = {}, 
    callback = '', 
    useLayer = 1,
    hasLoading = 1,
    contentType="application/x-www-form-urlencoded; charset=UTF-8",
    processData=true,
    isasync = true,
) {
    var indexLoading;
    let data = oData.param ? oData.param : (oData ? oData : '');
    if (typeof data == 'object') {
        data._token_2 = Date.now();
    }else{
        data += "&_token_2="+Date.now();
    }
    // console.log(data);
    // return false;
    $.ajax({
        type: type,
        url: url,
        // dataType: 'json',
        data:data,
        async: isasync,
        cache: false, 
        contentType: contentType,
        processData: processData, 
        headers:{
            'X-CSRF-TOKEN': $('meta[name=csrf-token]').attr('content')
        },
        success: function (data) {
            if (useLayer == 1) {
                let msg = '';
                if (data == 0 || data.code == 0) {
                    msg = data.msg == undefined ? '操作失败！' : data.msg;
                    layer.msg(msg);
                } else if (data > 0 || data.code > 0) {
                    msg = data.msg == undefined ? '操作成功！' : data.msg;
                    layer.msg(msg);
                } else {
                    layer.msg(data);
                }
            }
            if (callback !== '') callback(data);
        },
        error: function (data) {
            layer.msg(data);
        },
        beforeSend: function () {
            //0.4透明度 白色遮罩
            if (hasLoading == 1) {
                indexLoading = layer.load(2, {shade: [0.05, '#000']});
            }
        },
        complete: function () {
            if (hasLoading == 1) {
                layer.close(indexLoading);
            }
        },
    });
}

// 获取列表中的选中项，并返回id数组
function getDataIds() {
    let idsAry = [];
    $('input:checkbox:checked','tbody').each(function() {
        // console.log($(this).val());
        idsAry.push($(this).val());
    });
    if(idsAry.length === 0){
        layer.alert('请先选择要操作的数据');
        return false;
    }else{
        return idsAry;
    }
}

$('#checkAll').on('click',function () {
    // let toCheckStat = $(this).prop('checked');
    let divScroll = $(this).closest('div.dataTables_scroll');
    var obj = $(this).closest('table').find('tbody input:checkbox');
    if ($(divScroll).length > 0) {
        obj = $(divScroll).find('div.dataTables_scrollBody tbody input:checkbox');
    }
    $(obj).prop('checked', $(this).prop('checked'));
})

function ajaxForm(type='get', url='', formId = 'myform', callback = '') {
    var indexLoading;
    $.ajax({
        type: type,
        url: url,
        // dataType: 'json',
        data: $('#' + formId).serialize(),
        success: function (data) {
            let msg = '';
            if (data == 0 || data.code == 0) {
                msg = data.msg == undefined ? '操作失败！' : data.msg;
                layer.msg(msg);
            } else if (data == 1 || data.code == 1) {
                msg = data.msg == undefined ? '操作成功！' : data.msg;
                layer.msg(msg);
            } else {
                layer.msg(data);
            }
            callback(data);
        },
        error: function (data) {
            ajax_error(data);
        },
        beforeSend: function () {
            //0.4透明度 白色遮罩
            indexLoading = layer.load(2, {shade: [0.05, '#000']});
        },
        complete: function () {
            layer.close(indexLoading);
        },
    });
}

/*跳到指定页码*/
$('#jumpPage').on('change', function () {
    var info = table.page.info();
    var pageNumber = parseInt($(this).val());
    // console.log(pageNumber);
    if (pageNumber > 0 && pageNumber <= info.pages) {
        table.page(pageNumber - 1).draw(false);
    }
})

String.prototype.trim = function (char, type) {
    if (char) {
        if (type == 'left') {
            return this.replace(new RegExp('^\\' + char + '+', 'g'), '');
        } else if (type == 'right') {
            return this.replace(new RegExp('\\' + char + '+$', 'g'), '');
        }
        return this.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
    }
    return this.replace(/^\s+|\s+$/g, '');
};

function downloadIamge(imgsrc, name) {//下载图片地址和图片名
    let image = new Image();
    // 解决跨域 Canvas 污染问题
    image.setAttribute("crossOrigin", "anonymous");
    image.onload = function() {
        let canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        let context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
        let a = document.createElement("a"); // 生成一个a元素
        let event = new MouseEvent("click"); // 创建一个单击事件
        a.download = name || "photo"; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
    };
    image.src = imgsrc;
}

function getUrlParam(url, name) {
    var reg = new RegExp("(^|)" + name + "=([^&]*)(|$)"),data = url.match(reg);
    if(data){
        return data[2];
    }
    return null;
}

/*获取字符长度：中文按3个字符计算*/
function getStrLen(str){
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i); //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            len++;
        } else {
            len += 3;
        }
    }
    return len;
}